﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NetMQDemoPublisher
{
    public partial class PublisherForm : Form
    {
        private bool _send = false;
        private IPublisher publisher;
        public PublisherForm()
        {
            InitializeComponent();
            publisher = new Publisher("tcp://127.0.0.1:8888");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PublisherMq();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!_send)
            {
                timer1.Start();
                button2.Text = "取消定时发送";
            }
            else
            {
                timer1.Stop();
                button2.Text = "定时发送";
            }
            _send = !_send;
        }

        private void Timer1OnTick(object sender, EventArgs eventArgs)
        {
            PublisherMq();
        }

        private void PublisherMq()
        {
            string strContent = textBox1.Text;
            ListViewItem item = new ListViewItem(string.Format("topic:NetMQ,Data:{0}", strContent));
            listView1.Items.Add(item);
            publisher.Publish("NetMQ", strContent);
        }

        private void PublisherForm_Load(object sender, EventArgs e)
        {
            timer1.Tick += Timer1OnTick;
            if (!_send)
            {
                button2.Text = "定时发送";
            }
            else
            {
                button2.Text = "取消定时发送";
            }
        }
    }
}
